import sys
from bs4 import BeautifulSoup
import urllib.request as urllib2
import urllib
import re
import os
import save_sm_mid_imgs as saveIm
import saveToDB as DB
from datetime import datetime

print('---------------------- start download -------------------------------')
print(str(datetime.now()))
url = 'http://www.iz-dereva.com/project.php?id=3'


response = urllib2.urlopen(url)
html = response.read()

soup = BeautifulSoup(html)

img_classes = soup.findAll('td', {'align': 'center','valign':'top','style':'border:#ffffff 1px double'})







i = 0
for im in img_classes:
    #print(im)
    #im = img_classes[0]

#print(im.prettify())

    img = im.findNext('img')

    img_url = img.get('src')

    newpath = r'stairs_photos'

    if not os.path.exists(newpath): 
        os.makedirs(newpath)

    #print(img_url)

    regexp_pettern = '((\d)|(\w)|(\-))+(.jpg){1}$'

    tmp_image_name = re.search(regexp_pettern, img_url.lower())

    if tmp_image_name:
        image_name = tmp_image_name.group(0)
        print("iteration = " + str(i))
        print("    " + image_name)
        print("    " + img_url)


        image_on_web = urllib2.urlopen(img_url)
        buf = image_on_web.read()
        path_to_file = newpath + '\\'+image_name

        downloaded_image = open(path_to_file, "wb")
        downloaded_image.write(buf)
        downloaded_image.close()
        image_on_web.close()



        i = i+1
    #exit()


    # image_on_web = urllib2.urlopen(img_url)
    # buf = image_on_web.read()
    # path_to_file = newpath + '\\'+image_name


    # downloaded_image = open(path_to_file, "wb")
    # downloaded_image.write(buf)
    # downloaded_image.close()
    # image_on_web.close()